<template>
  <view>
    <view class="wrap">
      <view class="u-tabs-box">
        <u-tabs
          activeColor="#f29100"
          ref="tabs"
          :list="state.list"
          :current="state.current"
          @change="change"
          :is-scroll="false"
          swiperWidth="750"
        ></u-tabs>
      </view>
      <swiper
        class="swiper-box"
        :current="state.swiperCurrent"
        @transition="transition"
        @animationfinish="animationfinish"
      >
        <swiper-item class="swiper-item">
          <scroll-view scroll-y style="height: 100%; width: 100%" @scrolltolower="reachBottom">
            <view class="page-box">
              <view class="order" v-for="(res, index) in state.orderList" :key="res.id">
                <view class="top">
                  <view class="left">
                    <u-icon name="home" :size="30" color="rgb(94,94,94)"></u-icon>
                    <view class="store">{{ res.store }}</view>
                    <u-icon name="arrow-right" color="rgb(203,203,203)" :size="26"></u-icon>
                  </view>
                  <view class="right">{{ res.deal }}</view>
                </view>
                <view class="item" v-for="(item, index) in res.goodsList" :key="index">
                  <view class="left">
                    <image :src="item.goodsUrl" mode="aspectFill"></image>
                  </view>
                  <view class="content">
                    <view class="title u-line-2">{{ item.title }}</view>
                    <view class="type">{{ item.type }}</view>
                    <view class="delivery-time">发货时间 {{ item.deliveryTime }}</view>
                  </view>
                  <view class="right">
                    <view class="price">
                      ￥{{ priceInt(item.price) }}
                      <text class="decimal">.{{ priceDecimal(item.price) }}</text>
                    </view>
                    <view class="number">x{{ item.number }}</view>
                  </view>
                </view>
                <view class="total">
                  共{{ totalNum(res.goodsList) }}件商品 合计:
                  <text class="total-price">
                    ￥{{ priceInt(totalPrice(res.goodsList)) }}.
                    <text class="decimal">{{ priceDecimal(totalPrice(res.goodsList)) }}</text>
                  </text>
                </view>
                <view class="bottom">
                  <view class="more"
                    ><u-icon name="more-dot-fill" color="rgb(203,203,203)"></u-icon
                  ></view>
                  <view class="logistics btn">查看物流</view>
                  <view class="exchange btn">卖了换钱</view>
                  <view class="evaluate btn" @click="handleRemind">提醒发货</view>
                </view>
              </view>
              <u-loadmore :status="state.loadStatus[0]" bgColor="#f2f2f2"></u-loadmore>
            </view>
          </scroll-view>
        </swiper-item>
      </swiper>
    </view>
  </view>
</template>

<script setup>
  import { SubscribeTempIdEnum } from '@/enums/subscribe';

  const state = reactive({
    orderList: [],
    list: [
      {
        name: '待付款',
      },
      {
        name: '待发货',
      },
      {
        name: '待收货',
      },
      {
        name: '待评价',
        count: 12,
      },
    ],
    current: 0,
    swiperCurrent: 0,
    tabsHeight: 0,
    dx: 0,
    loadStatus: ['loadmore', 'loadmore', 'loadmore', 'loadmore'],
  });
  const handleRemind = () => {
    uni.requestSubscribeMessage({
      tmplIds: [SubscribeTempIdEnum.customerOrder],
      success(resp) {
        console.log(resp);
      },
      fail(err) {
        console.log(err);
      },
    });
  };
  // 页面数据
  const getOrderList = (idx) => {
    for (let i = 0; i < 5; i++) {
      let index = this.$u.random(0, this.dataList.length - 1);
      let data = JSON.parse(JSON.stringify(this.dataList[index]));
      data.id = this.$u.guid();
      this.orderList[idx].push(data);
    }
    this.loadStatus.splice(this.current, 1, 'loadmore');
  };
  // 总价
  const totalPrice = (item) => {
    let price = 0;
    item?.map((val) => {
      price += parseFloat(val.price);
    });
    return price?.toFixed(2);
  };

  const priceDecimal = (item) => {
    return 0;
  };

  const priceInt = (item) => {
    return item && 0;
  };

  const totalNum = (item) => {
    return 0;
  };

  // tab栏切换
  const change = (index) => {
    this.swiperCurrent = index;
    this.getOrderList(index);
  };
  const transition = ({ detail: { dx } }) => {
    this.$refs.tabs.setDx(dx);
  };
  const animationfinish = ({ detail: { current } }) => {
    this.$refs.tabs.setFinishCurrent(current);
    this.swiperCurrent = current;
    this.current = current;
  };
</script>

<style>
  /* #ifndef H5 */
  page {
    height: 100%;
    background-color: #f2f2f2;
  }

  /* #endif */
</style>

<style lang="scss" scoped>
  .order {
    width: 710rpx;
    background-color: #ffffff;
    margin: 20rpx auto;
    border-radius: 20rpx;
    box-sizing: border-box;
    padding: 20rpx;
    font-size: 28rpx;

    .top {
      display: flex;
      justify-content: space-between;

      .left {
        display: flex;
        align-items: center;

        .store {
          margin: 0 10rpx;
          font-size: 32rpx;
          font-weight: bold;
        }
      }

      .right {
        color: $u-warning-dark;
      }
    }

    .item {
      display: flex;
      margin: 20rpx 0 0;

      .left {
        margin-right: 20rpx;

        image {
          width: 200rpx;
          height: 200rpx;
          border-radius: 10rpx;
        }
      }

      .content {
        .title {
          font-size: 28rpx;
          line-height: 50rpx;
        }

        .type {
          margin: 10rpx 0;
          font-size: 24rpx;
          color: $u-tips-color;
        }

        .delivery-time {
          color: #e5d001;
          font-size: 24rpx;
        }
      }

      .right {
        margin-left: 10rpx;
        padding-top: 20rpx;
        text-align: right;

        .decimal {
          font-size: 24rpx;
          margin-top: 4rpx;
        }

        .number {
          color: $u-tips-color;
          font-size: 24rpx;
        }
      }
    }

    .total {
      margin-top: 20rpx;
      text-align: right;
      font-size: 24rpx;

      .total-price {
        font-size: 32rpx;
      }
    }

    .bottom {
      display: flex;
      margin-top: 40rpx;
      padding: 0 10rpx;
      justify-content: space-between;
      align-items: center;

      .btn {
        line-height: 52rpx;
        width: 160rpx;
        border-radius: 26rpx;
        border: 2rpx solid $u-border-color;
        font-size: 26rpx;
        text-align: center;
        color: $u-info-dark;
      }

      .evaluate {
        color: $u-warning-dark;
        border-color: $u-warning-dark;
      }
    }
  }

  .centre {
    text-align: center;
    margin: 200rpx auto;
    font-size: 32rpx;

    image {
      width: 164rpx;
      height: 164rpx;
      border-radius: 50%;
      margin-bottom: 20rpx;
    }

    .tips {
      font-size: 24rpx;
      color: #999999;
      margin-top: 20rpx;
    }

    .btn {
      margin: 80rpx auto;
      width: 200rpx;
      border-radius: 32rpx;
      line-height: 64rpx;
      color: #ffffff;
      font-size: 26rpx;
      background: linear-gradient(270deg, rgba(249, 116, 90, 1) 0%, rgba(255, 158, 1, 1) 100%);
    }
  }

  .wrap {
    display: flex;
    flex-direction: column;
    height: calc(100vh - var(--window-top));
    width: 100%;
  }

  .swiper-box {
    flex: 1;
  }

  .swiper-item {
    height: 100%;
  }
</style>
